<!--
 * @Author: yao
 * @Date: 2024-11-20 16:20:48
 * @LastEditTime: 2025-01-31 18:30:17
 * @LastEditors: yao
 * @Description: 
 * @FilePath: \jdev-web-el\src\components\AISpeaker\index.vue
-->
<template>
  <div class="AI_speaker">
    <div class="AI_speaker_switch" @click="switchMenu()">
      <el-icon>
        <ArrowUpBold v-if="isShowMenu" />
        <ArrowDownBold v-else />
      </el-icon>
    </div>
    <div class="AI_header">
      <div class="ai_module">
        <AIModule @click="handleButtonClick"></AIModule>
      </div>
      <div class="ai_content" ref="textElement" :class="['ai_' + useAI.speaker.state]">
        {{ useAI.speaker.content }}
      </div>
    </div>
    <div class=" AI_container" :class="{ 'AI_container_expend': isShowMenu }">
    </div>
  </div>
</template>
<script setup lang='ts'>
import { useAIStore } from '@/stores/modules/AI'
import { ref, onMounted, watch } from 'vue'
const useAI = useAIStore()
const isShowMenu = ref(false)
const switchMenu = () => {
  isShowMenu.value = !isShowMenu.value
}
const textElement = ref(null)
const name = ref("小助手");
const state = ref("info");
const message = ref(`你好，我是${name.value}。`);

const messages: any = {
  info: "你好，我是你的助手。",
  error: "哎呀，出错了。让我看看是什么问题。",
  reminder: "你已经工作很久了，记得休息一下哦。",
  confused: "你点击了很多次，需要帮助吗？",
  success: "操作成功！",
  warning: "请注意，这可能不是你想要的操作。",
};

const clickCount = ref(0);

function updateMessage(newState) {
  state.value = newState;
  message.value = messages[newState];
}

function handleError(error) {
  updateMessage("error");
  console.error(`错误信息: ${error.message}`);
}

function handleIdle() {
  useAI.setContent("有什么需要帮助的么？如果有需要请展开AI选项卡详细提问~", 10000);
}

function handleReminder() {
  useAI.setContent("是时候休息休息了~", -1);
  useAI.setContent("快点使用我，否则我还有xx分钟就要退出登录了~！！", -1);
}

function handleClickTooMuch() {
  updateMessage("confused");
  clickCount.value = 0; // 重置计数
}

function handleSuccess() {
  updateMessage("success");
}

function handleWarning() {
  updateMessage("warning");
}

function handleButtonClick() {
  clickCount.value++;
  if (clickCount.value > 5) {
    handleClickTooMuch();
  } else {
    handleIdle();
  }
}

let reminderInterval: any = null;

function startReminder(interval) {
  stopReminder();
  reminderInterval = setInterval(
    () => {
      handleReminder();
    },
    interval * 60 * 1000
  ); // 每隔interval分钟提醒一次
}

function stopReminder() {
  if (reminderInterval) {
    clearInterval(reminderInterval);
    reminderInterval = null;
  }
}

onMounted(() => {
  startReminder(1); // 每15分钟提醒一次
});

</script>
<style scoped lang='scss'>
.AI_speaker {
  font-weight: 500;
  font-size: 1rem;
  margin-right: 20px;
  border-bottom-left-radius: 12px;
  border-bottom-right-radius: 12px;
  backdrop-filter: blur(10px);
  box-shadow: var(--ep-box-shadow);
  background: rgba(255, 255, 255, 0.4);
  backdrop-filter: blur(10px);

  .AI_header {
    // background: linear-gradient(147.38deg, #0c8dc9 0%, #0b7fb7 100%);

    border-bottom-left-radius: 12px;
    border-bottom-right-radius: 12px;
    display: flex;
    align-items: center;
    padding: 10px 6px 10px 16px;
    height: 40px;

    .ai_module {
      width: 3rem;
      height: 2.3rem;
    }

    .ai_content {
      border-radius: 8px;
      padding: .5rem 1rem;
      margin: 0 1rem;
      font-weight: 700;
      width: max-content;
      position: relative;
    }


    .ai_warning {
      background-color: var(--ep-color-warning-light-9);
      color: var(--ep-color-warning);
    }

    .ai_success {
      background-color: var(--ep-color-success-light-9);
      color: var(--ep-color-success);
    }

    .ai_error {
      background-color: var(--ep-color-error-light-9);
      color: var(--ep-color-error);
    }

    .ai_normal {
      background-color: var(--ep-color-info-light-9);
      color: var(--ep-color-info);
    }
  }

  .AI_container {
    height: 0;
    transition: height 0.2s ease-in-out;
    overflow: hidden;
  }

  .AI_container_expend {
    height: calc(100vh - 160px);
    transition: height 0.2s ease-in-out;
  }

  .AI_speaker_switch {
    position: absolute;
    bottom: -17px;
    right: 44px;
    color: var(--ep-primary-color);
    padding: 0 14px;
    border-bottom-left-radius: 6px;
    border-bottom-right-radius: 6px;
    box-shadow: var(--ep-box-shadow);
    background-color: #f1f5f9ac;
    cursor: pointer;
    line-height: 13px;
  }


}
</style>